<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

$officer_id = -1;
if( isset( $_GET[ 'officer_id' ] ) && is_numeric( $_GET[ 'officer_id' ] ) ) {
	$officer_id = $_GET[ 'officer_id' ];
}

$location_id = -1;
if( isset( $_GET[ 'location_id' ] ) && is_numeric( $_GET[ 'location_id' ] ) ) {
	$location_id = $_GET[ 'location_id' ];
	$query = "SELECT faction_id " .
			"FROM faction_member " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'faction_id' ] ) ) {
		$query = "SELECT * " .
				"FROM locations " .
				"WHERE ruler_id IN ( SELECT ruler_id FROM faction_member WHERE faction_id = " . $row[ 'faction_id' ] . " AND requested = 0 AND invited = 0 ) " .
					" AND location_id = $location_id";
	}
	else {
		$query = "SELECT * " .
				"FROM locations " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND location_id = $location_id";
	}
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'location_id' ] ) ) {
		$x = $row[ 'x' ];
		$y = $row[ 'y' ];
	}
	else {
		$status = translate( 'Invalid location selected.' );
	}
}

if( !isset( $status ) && !isset( $x ) ) {
	if( isset( $_GET[ 'x' ] ) && is_numeric( $_GET[ 'x' ] ) && isset( $_GET[ 'y' ] ) && is_numeric( $_GET[ 'y' ] ) ) {
		$x = $_GET[ 'x' ];
		$y = $_GET[ 'y' ];
	}
	else {
		$status = translate( 'No valid coordinates or location selected.' );
	}
}

if( !isset( $status ) ) {
	$query = "SELECT faction_id " .
			"FROM faction_member " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	
	if( !isset( $row[ 'faction_id' ] ) ) {
		$faction_id = -1;
	}
	else {
		$faction_id = $row[ 'faction_id' ];
	}
	
	if ($faction_id != -1 ) {
		$query = "SELECT DISTINCT army.* " .
		"FROM army_visible " .
		"JOIN army ON army.army_id = army_visible.army_id " .
		"WHERE army_visible.ruler_id IN ( SELECT faction_member.ruler_id FROM faction_member WHERE faction_id = $faction_id AND invited = 0 AND requested = 0 ) " .
			"AND actual_x + 0.5 >= $x AND actual_x - 0.5 < $x AND actual_y + 0.5 >= $y AND actual_y - 0.5 < $y " .
		"ORDER BY bombard DESC, name";
	} else {
		$query = "SELECT DISTINCT army.* " .
		"FROM army_visible " .
		"JOIN army ON army.army_id = army_visible.army_id " .
		"WHERE army_visible.ruler_id = " . $_SESSION['ruler_id'] . " " .
			"AND actual_x + 0.5 >= $x AND actual_x - 0.5 < $x AND actual_y + 0.5 >= $y AND actual_y - 0.5 < $y " .
		"ORDER BY bombard DESC, name";
	}
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$num = mysql_numrows($result);
	$i = 0;
	if( $num > 0 ) {
?>
			<strong><?php transprint( 'Select an army from' ); ?> <a href="../operations/map.php?x=<?php print( $x - 20 ); ?>&y=<?php print( $y - 20 ); ?>">(<?php print( $x ); ?>, <?php print( $y ); ?>)</a></strong><br />
			<ul>
<?php	
		while ($i < $num) {
			$query2 = "SELECT ( scout + infantry + cavalry + artillery ) AS army_size " .
					"FROM army " .
					"WHERE army_id = " . mysql_result($result, $i, 'army_id' );
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$sum = $row2[ 'army_size' ];
			
			if( mysql_result($result, $i, 'ruler_id' ) == $_SESSION[ 'ruler_id' ] ) {
				$color = 'green';
				$href = 'army.php?name=' . mysql_result($result, $i, 'name' );
				$onclick = '';
			}
			else {
				$query2 = "SELECT faction_id " .
						"FROM faction_member " .
						"WHERE ruler_id = " . mysql_result($result, $i, 'ruler_id' ) . " AND invited = 0 AND requested = 0";
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				if( $faction_id != -1 && isset( $row2[ 'faction_id' ] ) && $row2[ 'faction_id' ] == $faction_id ) {
					$color = 'blue';
				}
				else {
					$color = 'red';
				}
				$href = 'javascript:void(null);';
				$onclick = 'getArmyInformation( ' . mysql_result($result, $i, 'army_id' ) . ', ' . $officer_id . ' )';
			}
?>
				<li>
					<a href="<?php print( $href ); ?>" onclick="<?php print( $onclick ); ?>">
						<strong class="<?php print( $color ); ?>"><?php print( mysql_result($result, $i, 'name' ) ); ?> - <?php print( number_format( $sum ) ); ?></strong>
					</a>
				</li>
<?php	
			$i ++;
		}
?>
			</ul>
			<?php transprint( 'Click' ); ?> <a href="javascript:void(null)" onclick="backToSelectLocation( <?php print( $officer_id ); ?> );"><?php transprint( 'here' ); ?></a> <?php transprint( 'to go back to select a different location' ); ?>.
<?php	
	}
	else {
?>
			<?php transprint( 'Status' ) ?>: <?php transprint( 'There are no visible armies at this location.' ); ?><br />
			<br />
<?php
		print_officer_gather_info_page( $officer_id );
	}
}
else {
?>
			<?php transprint( 'Status' ) ?>: <?php  print( $status ); ?><br />
			<br />
<?php
	print_officer_gather_info_page( $officer_id );
}
?>
